import "reflect-metadata"
import "./common";
import "./android"

events.removeAllListeners()
events.setMaxListeners(1000)

import { container, container as injector } from 'tsyringe'
/**
 * 制作一个APP管理框架
 */
import { Task } from "./autox/task";
import { Sender } from "./autox/sender";
import { Robot } from "./rebot";
import { Timer } from "./autox/timer";
import { FloatyWindow } from "./autox/floatWindow";
import { $ } from "./utils";

const task = injector.resolve(Task)
const sender = injector.resolve(Sender)
const timer = injector.resolve(Timer)
const floatWindow = injector.resolve(FloatyWindow)

timer.setInterval(() => { }, 1000)
events.on(`exit`, () => {
    console.hide()
    injector.dispose()
    emitter.removeAllListeners()
    events.removeAllListeners()
})

/**
 * com.yxcorp.gifshow.HomeActivity
 * com.yxcorp.gifshow.detail.PhotoDetailActivity
 * com.kuaishou.live.core.basic.activity.LiveSlideActivity
 * com.kuaishou.live.krn.dialog.container.LiveKrnBottomDialogFragment$c
 * n27.a 红包/抽奖/个人主页
 * com.kuaishou.live.basic.dialog.LiveDialogContainerFragment$b
 * gqc.a
 * com.yxcorp.plugin.search.SearchActivity 搜索页
 * zd4.q 领取并使用
 */
export const creater = (context: any, name: string) => {
    const property = () => Object.keys(context).map(key => {
        try {
            const val = context[key];
            const c = `${val}`.replace('\/*', '').replace('*\/', '');
            if (typeof val === 'function') {
                return `/*****\n${c}\n*****/\n\t${key}(){\n\treturn ${name}.${key}()\n}`
            } else if (typeof val === 'boolean') {
                return `/*****\n${c}\n*****/\n\tget ${key}(): boolean{\n\treturn ${name}.${key}\n}`
            } else if (typeof val === 'number') {
                return `/*****\n${c}\n*****/\n\tget ${key}(): number{\n\treturn ${name}.${key}\n}`
            } else {
                return `/*****\n${c}\n*****/\n\tget ${key}(): string{\n\treturn ${name}.${key}\n}`
            }
        } catch (e) {
            log(e.message)
            return `//${key} error ${e.message}`
        }
    }).join(`\n`)
    const code = `export class Core${capitalizeFirstLetter(name)} {\n${property()}\n}`
    return code;
}

function capitalizeFirstLetter(str: string) {
    return str.charAt(0).toUpperCase() + str.slice(1);
}
// console.log(creater(context, `context`))
// console.log(creater(runtime, `runtime`))
declare const runtime: any;
const emitter = sender.create()
const robot = container.resolve(Robot)
render();
sleep(1000)
// render()
emitter.on(`ACTIVITY_CHANGE`, (latestActivity: string, latestPackage: string) => {
    log(`${latestActivity}-${latestPackage}`)
    robot.onActivity(latestActivity, latestPackage)
});
const t = task.start(() => {
    let _currentActivity: string;
    runtime.info.useShell = true;
    while (true) {
        const latestActivity = runtime.info.latestActivity;
        const latestPackage = runtime.info.latestPackage;
        if (_currentActivity !== latestActivity) {
            _currentActivity = latestActivity;
            emitter.emit('ACTIVITY_CHANGE', latestActivity, latestPackage)
        }
        sleep(5000)
    }
})
t.waitFor()
app.launchPackage($.PACKAGES["com.smile.gifmaker"])
console.show();
function render() {
    floatWindow.render(`<frame id="main" w="*" h="*" bg="#000000" alpha="0">
        <vertical w="*" h="*">
            <horizontal padding="15 5 15 5">
                <text w="*" padding="15 5 15 5" bg="#130B2E" h="auto" id="toast" textSize="16" gravity="center" text="" textColor="#D12E2E"/>
            </horizontal>
            <horizontal padding="15 5 15 5">
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="当前应用：" textColor="#E2E1CF"/>
                    <text w="*" id="currentApp" textSize="14" gravity="left" text="-" textColor="#F2F2F2"/>
                </horizontal>
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="当前页面：" textColor="#E2E1CF"/>
                    <text w="*" id="currentAppPage" textSize="14" gravity="left" text="-" textColor="#F2F2F2"/>
                </horizontal>
            </horizontal>
            <horizontal padding="15 5 15 5">
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="当前任务：" textColor="#E2E1CF"/>
                    <text w="*" id="currentTask" textSize="14" gravity="left" text="-" textColor="#F2F2F2"/>
                </horizontal>
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="所在房间：" textColor="#E2E1CF"/>
                    <text w="*" id="currentRoom" textSize="14" gravity="left" text="-" textColor="#F2F2F2"/>
                </horizontal>
            </horizontal>
            <horizontal padding="15 5 15 5">
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="执行状态：" textColor="#F2F2F2"/>
                    <text w="*" id="currentStatus" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
                </horizontal>
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="执行结果：" textColor="#F2F2F2"/>
                    <text w="*" id="currentResult" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
                </horizontal>
            </horizontal>
            <horizontal padding="15 5 15 5">
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="快手：" textColor="#F2F2F2"/>
                    <text w="*" id="kwaiCurrentMoney" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
                </horizontal>
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="分配：" textColor="#F2F2F2"/>
                    <text w="*" id="kwaiTodayCount" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
                </horizontal>
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="快币：" textColor="#F2F2F2"/>
                    <text w="*" id="kwaiTodayMoney" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
                </horizontal>
            </horizontal>
            <horizontal padding="15 5 15 5">
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="极速：" textColor="#F2F2F2"/>
                    <text w="*" id="nebulaCurrentMoney" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
                </horizontal>
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="分配：" textColor="#F2F2F2"/>
                    <text w="*" id="nebulaTodayCount" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
                </horizontal>
                <horizontal layout_weight="1">
                    <text w="auto" textSize="12" gravity="left" text="快币：" textColor="#F2F2F2"/>
                    <text w="*" id="nebulaTodayMoney" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
                </horizontal>
            </horizontal>
            <horizontal padding="15 5 15 5">
                <text w="*" id="tip" textSize="12" gravity="center" text="-" textColor="#f29a8c"/>
            </horizontal>
            <horizontal h="50"></horizontal>
        </vertical>
    </frame>`)
    let state = {}
    const updateUi = (state: any) => {
        ui.run(() => {
            Object.keys(state).map(key => {
                const val = state[key]
                if (val) {
                    floatWindow.window[key].setText(state[key])
                } else {
                    log(`${key}:${val}`)
                }
            })
        })
    }
    floatWindow.on('UPDATE_FLOAT_WINDOW_STATE_CHANGE', (val: any) => {
        state = {
            currentTask: '抢包',
            ...state,
            ...val,
        };
        updateUi(state)
    });
}

